package com.example.likestarlibrary;

import com.example.likestarlibrary.util.NumCalcUtil;
import ohos.agp.components.AttrSet;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.PixelMapElement;
import ohos.app.Context;
import ohos.global.resource.Element;
import ohos.global.resource.Resource;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;

import java.util.Random;

public class TCHeartLayout extends DependentLayout {

    private static Element[] sDrawables;
    private int[] drawableIds = new int[]{ResourceTable.Media_heart1, ResourceTable.Media_heart2, ResourceTable.Media_heart3, ResourceTable.Media_heart4, ResourceTable.Media_heart5, ResourceTable.Media_heart6
    };
    private TCAbstractPathAnimator mAnimator;
    private int textHight;
    private int dHeight;
    private int dWidth;
    private int initX;
    private int pointx;
    //动画持续时间
    private int animalTime = 1000;
    private AttrSet attrs;
    //自定义图片颜色
    private int imgColor = 0;
    //private Random mRandom = new Random();
    final double val = Math.random();
    final int sval = (int)(val*5);
    private PixelMap[] mHearts;
    private PixelMapElement[] mHeartsDrawable;

    public TCHeartLayout(Context context, AttrSet attrSet) {
        super(context, attrSet);
        mContext = context;
        attrs = attrSet;
        try {
            findViewById(context);
            initHeartDrawable();
        } catch (Exception e) {
            e.printStackTrace();
        }
        init(attrSet);
    }

    private void findViewById(Context context) throws Exception {
        LayoutScatter.getInstance(context).parse(ResourceTable.Layout_likestar_main, this, true);
        Resource resource = getResourceManager().getResource(ResourceTable.Media_heart1);
        PixelMap bitmap = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
        dHeight = bitmap.getImageInfo().size.width;
        dWidth = bitmap.getImageInfo().size.height;
        textHight = (int)(NumCalcUtil.add((float) PxUtil.fp2px(20),(float)dHeight / 2));
        //随机上浮方向的x坐标
        pointx = dWidth;
        bitmap.release();
    }

    private void init(AttrSet attrs) {
        this.initX = 30;
        if (pointx <= this.initX && pointx >= 0) {
            pointx -= 10;
        } else if (pointx >= -this.initX && pointx <= 0) {
            pointx += 10;
        } else {

            pointx = this.initX;
        }

        TCAbstractPathAnimator.Config config = TCAbstractPathAnimator.Config
                .fromTypeArray(attrs, this.initX, textHight, pointx, dWidth, dHeight);
        mAnimator = new TCPathAnimator(config);
    }

    public void resourceLoad() throws Exception {
        mHearts = new PixelMap[drawableIds.length];
        mHeartsDrawable = new PixelMapElement[drawableIds.length];
        for (int i = 0; i < drawableIds.length; i++) {
            Resource resource = getResourceManager().getResource(drawableIds[i]);
            mHearts[i] = ImageSource.create(resource, new ImageSource.SourceOptions()).createPixelmap(null);
            mHeartsDrawable[i] = new PixelMapElement(mHearts[i]);
        }
    }

    private void initHeartDrawable() throws Exception {
        int size = drawableIds.length;
        sDrawables = new Element[size];
        for (int i = 0; i < size; i++) {
            sDrawables[i] = getResourceManager().getElement(drawableIds[i]);
        }
        resourceLoad();
    }

    public void addFavor() {
        TCHeartView heartView = new TCHeartView(getContext());
        if (imgColor != 0) {
            int drawableId = drawableIds[sval];
            heartView.setColorAndDrawables(getImgColor(), drawableId, drawableId);
        } else {
            heartView.setDrawable(mHeartsDrawable[sval]);
        }
        mAnimator.start(heartView, this);
    }

    public int getImgColor() {
        return imgColor;
    }

    public void setImgColor(int imgColor) {
        this.imgColor = imgColor;
    }


    public int[] getDrawableIds() {
        if(drawableIds == null){
            return null;
        }
        return (int[])drawableIds;
    }


    public int getAnimalTime() {
        return animalTime;
    }

    public void setAnimalTime(int animalTime) {
        this.animalTime = animalTime;
        init(attrs);
    }

}
